package servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;
import managers.JSonManager;
import structures.AD;

public class GetADServlet extends HttpServlet {

	private static final long serialVersionUID = -53735661142306131L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		String idAdvertisement, jsonResult;
		AD advertisement;
		JSonManager jSonManager;
		jSonManager = new JSonManager();
		DatabaseManager databaseManager;
		databaseManager = new DatabaseManager();
		idAdvertisement = request.getParameter("idAdvertisement");
		
         try {
        	databaseManager.connect();
        	advertisement = databaseManager.getADByIdAdvertisement(idAdvertisement);
        	
        	jsonResult = jSonManager.generateJSON(advertisement);
        	response.setContentType("application/json");
    		response.setCharacterEncoding("UTF-8");
			response.getWriter().write(jsonResult);
			databaseManager.closeConnection();
	        
        } catch (Exception e) {
			e.printStackTrace();
		}
    }
}
